package com.example.music_review;

import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.music_review.adapter.WelcomeAdapter;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private RadioGroup radioGroup;
    private ViewPager viewPager;
    private RadioButton one;
    private RadioButton two;
    private List<Fragment> list = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        radioGroup = findViewById(R.id.rg);
        viewPager = findViewById(R.id.viewPager);
        one = findViewById(R.id.one);
        two = findViewById(R.id.two);
        list.add(new Fragment1());
        list.add(new Fragment2());

        viewPager.setAdapter(new WelcomeAdapter(getSupportFragmentManager(), list));

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (one.getId() == checkedId) {
                    viewPager.setCurrentItem(0);
                } else if (two.getId() == checkedId) {
                    viewPager.setCurrentItem(1);
                }
            }
        });

        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {
            }

            @Override
            public void onPageSelected(int i) {
                if (i == 0) {
                    one.setChecked(true);
                } else if (i == 1) {
                    two.setChecked(true);
                }
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
    }
}
